{% extends "master.html" %}
{% block subtitle %} - 主面板 - 仓位管理 - 内部转仓{% endblock %}

{% block extJS %}
<script language="JavaScript" type="text/javascript">
//<![CDATA[
    $(document).ready(function(){
        $(".item_cb").click(function(){
            var t = $(this);
            var qtyname = t.attr('name').replace('items_','qty_');
            $("#"+qtyname).val($("input[name='"+t.attr('name')+"']:checked").length);
        });
    })


    function toSbumit(){
        var msg = [];
        clearMsg();
        
        $(".err").removeClass("err");
        var flag = true;
        var qtyMatch = true;
        $(".num").each(function(){
            var t = $(this);
            if(!check_number(t.val())){
                flag = false;
                t.addClass('err');
            }else{
                var name = t.attr('name').replace('qty_','items_');
                var cs = $("input[name='"+name+"']:checked").length
                if(parseInt(t.val()) != cs){
                    qtyMatch = false;
                    t.addClass('err');
                }
            }
        })
        
        if(!flag){ msg.push('请正确填写数量！'); }
        
        var allIvtOK = true;
        $("select[name^='divt_']").each(function(){
            var t = $(this);
            if(!t.val()){
                allIvtOK = false;
                t.addClass("err");   
            }
        });
        
        if(!allIvtOK){ msg.push('请正确选择要转移的仓位！'); }
        
        if(!qtyMatch){ msg.push('请选择相关的商品个体，所选数量必须与转仓数量相符！');}
  
        if(msg.length > 0){
            alertError(msg);
            return false;
        }else{    
            $("form").submit();  
            
        }        
    }
    
    function toCancel(){
        redirect_alert("{{MSG_LEAVE_WITHOUT_SAVING}}","{{url_for('bpIvt.view')}}")
    } 
//]]>
</script>
{% endblock %}

{% block main %}
<div class="container">
    <div class="row">
        <ol class="breadcrumb">
          <li><a href="{{url_for('bpRoot.view')}}">主面板</a></li>
          <li><a href="{{url_for('bpIvt.view')}}">仓位管理</a></li>
          <li class="active">内部转仓</li>
        </ol>
    </div>
    <div class="row">
        <div class="btn-toolbar pull-right">
            <div class="btn-group"><button type="button" class="btn btn-primary" onclick="return toSbumit()">确认提交</button></div>
            <div class="btn-group"><button type="button" class="btn btn-inverse" onclick="return toCancel()">取消</button></div>
        </div>
    </div>
    <div class="row">
        <form method='post' action="{{url_for('.view',action='internalSave')}}" enctype="multipart/form-data">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <td class="col-md-3">商品编号</td>
                    <td>商品名称</td>
                    <td>现处仓位</td>
                    <td class="col-md-1">存量</td>
                    <td class="col-md-2">转移到仓位</td>
                    <td class="col-md-1">转仓数量</td>
                </tr>
            </thead>
            <tbody>
                {% for (pdt,r) in result %}
                    <tr>
                        <td>{{mymacros.render_goto_popup_pdt(pdt, loop.first)}}</td>
                        <td>{{pdt}}</td>
                        <td>{{r.ivt.fullPath}}</td>
                        <td>{{r.qty}}</td>
                        <td>
                            <select name="divt_{{r.pdtID}}_{{r.ivtID}}" id="divt_{{r.pdtID}}_{{r.ivtID}}">
                                <option value=""></option>
                                {% for ivt in ivts %}
                                    {% if ivt.id != r.ivtID %}
                                        <option value="{{ivt.id}}">{{ivt.fullPath}}</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </td>
                        <td>
                            <input type="text" name="qty_{{r.pdtID}}_{{r.ivtID}}" id="qty_{{r.pdtID}}_{{r.ivtID}}" value="0" class="num col-md-1" maxqty="{{r.qty}}"/>
                        </td>
                    </tr>
                    <tr id="tr_{{r.pdtID}}_{{r.ivtID}}">
                        <td colspan="3">该仓位&nbsp;[<strong>{{r.ivt.fullPath}}</  strong>]&nbsp;中的商品&nbsp;[<strong>{{pdt}}</strong>]&nbsp;个体 :</td>
                        <td colspan="3">
                            <ul class="list-unstyled">
                                {% set key = [pdt.id,r.ivtID]|join('_') %}
                                {% for item in items[key] %}
                                    <li><label for="item_{{item.id}}"><input type="checkbox" name="items_{{r.pdtID}}_{{r.ivtID}}" value="{{item.id}}" id="item_{{item.id}}" class="item_cb"/>{{item.no}}</label></li>
                                {% endfor %}
                            </ul>
                        </td>
                    </tr>
                {% endfor %}
            </tbody>                
        </table>
        </form>
    </div>
    
 
</div>
{% endblock %}